package com.lottery.ruok.lottrey.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import com.lottery.ruok.lottrey.R;
import com.lottery.ruok.lottrey.eventbusObject.CancelOrder;
import com.lottery.ruok.lottrey.eventbusObject.GetAccountWithPush;
import com.lottery.ruok.lottrey.retrofitModule.RetrofitBase;
import com.lottery.ruok.lottrey.retrofitModule.RxHelper;
import com.lottery.ruok.lottrey.retrofitModule.RxSubscribe;
import com.lottery.ruok.lottrey.retrofitModule.request.OrderCancelRequest;
import com.lottery.ruok.lottrey.retrofitModule.response.OrderCancelResponse;
import com.lottery.ruok.lottrey.userInfo.UserInfo;
import com.lottery.ruok.lottrey.utils.StringUtils;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import ui.LineRecordView;
import utils.Utils;
/*
* 投注详情
* */

public class BetDetailActivity extends BaseActivity {

    /*** Edem update on 20180115 ***/
    @BindView(R.id.order_id)
    LineRecordView orderId;
    /*** Edem update End ***/
    @BindView(R.id.cancel_order)
    TextView cancelOrder;

    @BindView(R.id.account_detail)
    LineRecordView accountDetail;
    @BindView(R.id.lottery_type)
    LineRecordView lotteryType;
    @BindView(R.id.play_motion)
    LineRecordView playMotion;
    @BindView(R.id.prize_number)
    LineRecordView prizeNumber;
    @BindView(R.id.prize_condition)
    LineRecordView prizeCondition;
    @BindView(R.id.bet_number)
    LineRecordView betNumber;
    @BindView(R.id.bet_money)
    LineRecordView betMoney;
    @BindView(R.id.bet_time)
    LineRecordView betTime;
    @BindView(R.id.bet_count)
    LineRecordView betCount;
    @BindView(R.id.bet_back)
    LineRecordView betBack;
    @BindView(R.id.odd)
    LineRecordView odd;
    @BindView(R.id.prize_money)
    LineRecordView prizeMoney;
    @BindView(R.id.prize_note)
    LineRecordView prize_note;
    @BindView(R.id.amount_per_note)
    LineRecordView amountPerNote;
    @BindView(R.id.profit)
    LineRecordView profit;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bet_detail);
        ButterKnife.bind(this);

        init();
    }

    private void init() {
        orderId.setLeftTitleColor("#989898");
        orderId.setRightTitleColor("#5b5b5b");
        accountDetail.setLeftTitleColor("#989898");
        accountDetail.setRightTitleColor("#5b5b5b");
        lotteryType.setLeftTitleColor("#989898");
        lotteryType.setRightTitleColor("#5b5b5b");
        playMotion.setLeftTitleColor("#989898");
        playMotion.setRightTitleColor("#5b5b5b");
        prizeNumber.setLeftTitleColor("#989898");
        prizeNumber.setRightTitleColor("#5b5b5b");
        prizeCondition.setLeftTitleColor("#989898");
        prizeCondition.setRightTitleColor("#5b5b5b");
        betNumber.setLeftTitleColor("#989898");
        betNumber.setRightTitleColor("#5b5b5b");
        betMoney.setLeftTitleColor("#989898");
        betMoney.setRightTitleColor("#5b5b5b");
        betTime.setLeftTitleColor("#989898");
        betTime.setRightTitleColor("#5b5b5b");
        betCount.setLeftTitleColor("#989898");
        betCount.setRightTitleColor("#5b5b5b");
        betBack.setLeftTitleColor("#989898");
        betBack.setRightTitleColor("#5b5b5b");
        odd.setLeftTitleColor("#989898");
        odd.setRightTitleColor("#5b5b5b");
        prizeMoney.setLeftTitleColor("#989898");
        prizeMoney.setRightTitleColor("#5b5b5b");
        prize_note.setLeftTitleColor("#989898");
        prize_note.setRightTitleColor("#5b5b5b");
        amountPerNote.setLeftTitleColor("#989898");
        amountPerNote.setRightTitleColor("#5b5b5b");
        profit.setLeftTitleColor("#989898");
        profit.setRightTitleColor("#5b5b5b");

        orderId.setRightTitle(getIntent().getStringExtra("order_id"));

        accountDetail.setRightTitle(getIntent().getStringExtra("no"));

        lotteryType.setRightTitle(getIntent().getStringExtra("lotteryType"));

        playMotion.setRightTitle(getIntent().getStringExtra("item_name"));

        prizeNumber.setRightTitle(getIntent().getStringExtra("number"));

        prizeCondition.setRightTitle(getIntent().getStringExtra("condition"));
        if (StringUtils.isNotEmpty(getIntent().getStringExtra("position"))){
            betNumber.setRightTitle("（"+getIntent().getStringExtra("position")+"）"+getIntent().getStringExtra("betNum"));
        }else {
            betNumber.setRightTitle(getIntent().getStringExtra("betNum"));
        }

        betTime.setRightTitle(getIntent().getStringExtra("bet_time"));

        betMoney.setRightTitle(getIntent().getStringExtra("bet_money") + "元");

        if(UserInfo.getEnableWithdrawal()==1){
            if (getIntent().getStringExtra("order_status").equals("1")) {
                cancelOrder.setVisibility(View.VISIBLE);
                cancelOrder.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        cancelOrder();
                    }
                });
            }
        }else{
            cancelOrder.setVisibility(View.GONE);
        }


        if (getIntent().getStringExtra("condition").equals("已中奖")) {
            betCount.setVisibility(View.VISIBLE);
            betBack.setVisibility(View.VISIBLE);
            odd.setVisibility(View.VISIBLE);
            prizeMoney.setVisibility(View.VISIBLE);
            prize_note.setVisibility(View.VISIBLE);
            prize_note.setRightTitle(getIntent().getStringExtra("win_note_count"));
            betCount.setRightTitle(getIntent().getStringExtra("bet_count"));
            betBack.setRightTitle(getIntent().getStringExtra("back"));
            odd.setRightTitle(getIntent().getStringExtra("odd"));
            prizeMoney.setRightTitle(getIntent().getStringExtra("win_amount") + "元");
            Float pers = Float.parseFloat(getIntent().getStringExtra("bet_money")) / Float.parseFloat(getIntent().getStringExtra("bet_count"));
            amountPerNote.setRightTitle(Utils.saveTwoFormat(pers) + "元");
            amountPerNote.setVisibility(View.VISIBLE);
            profit.setRightTitle(Utils.saveTwoFormat(Float.parseFloat(getIntent().getStringExtra("win_amount")) - Float.parseFloat(getIntent().getStringExtra("bet_money"))) + "元");
            profit.setVisibility(View.VISIBLE);
        }

        betNumber.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(BetDetailActivity.this, BetNumberActivity.class);
                intent.putExtra("betNum", getIntent().getStringExtra("betNum"));
                startActivity(intent);
            }
        });
    }

    private void cancelOrder() {
        Utils.createDialog(this, "是否取消订单", "确定", "取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                RetrofitBase.getRetrofit().create(OrderCancelRequest.class).getResult(getIntent().getStringExtra("order_id"), UserInfo.getToken())
                        .compose(RxHelper.<OrderCancelResponse>handleResult())
                        .subscribe(new RxSubscribe<OrderCancelResponse>(BetDetailActivity.this) {
                            @Override
                            protected void _onNext(OrderCancelResponse orderCancelResponse) {
                                EventBus.getDefault().post(new CancelOrder());
                                Utils.showToast("您已撤销订单");
                                if (orderCancelResponse.getAfter_amount() != 0) {
                                    UserInfo.setAccount(orderCancelResponse.getAfter_amount() + "");
                                    EventBus.getDefault().post(new GetAccountWithPush());
                                }

                                finish();
                            }

                            @Override
                            protected boolean showDialog() {
                                return true;
                            }
                        });
            }
        }, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
    }
}
